package studio.xiaoyun.web.restController;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import studio.xiaoyun.core.dao.IFeedbackDao;
import studio.xiaoyun.core.entity.Feedback;
import studio.xiaoyun.core.exception.CoreException;
import studio.xiaoyun.core.exception.ErrorCode;
import studio.xiaoyun.core.parameter.FeedbackParameter;
import studio.xiaoyun.core.service.IFeedbackService;
import studio.xiaoyun.web.ParameterUtil;
import studio.xiaoyun.web.ResourceUtil;
import studio.xiaoyun.web.RestResult;
import studio.xiaoyun.web.resource.FeedbackResource;

/**
 * 意见反馈的控制器
 * @author 岳正灵
 * @since 1.0.0
 */
@RestController
@RequestMapping("/v1/feedback")
public class FeedbackRestController {
	@Resource
	private IFeedbackDao feedbackDao;
	@Resource
	private ResourceUtil resourceUtil;
	@Resource
	private IFeedbackService feedbackService;
	
	/**
	 * 创建一个意见反馈
	 * @param title 标题
	 * @param text 文字
	 * @return 新创建的意见反馈
	 */
	@ResponseStatus(HttpStatus.CREATED)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public RestResult createFeedback(@RequestParam String title,@RequestParam String text){
		long ID = feedbackService.createFeedback(title, text);
		Feedback feedback = feedbackDao.getByID(ID);
		FeedbackResource resource = resourceUtil.toFeedbackResource(feedback,null);
		return new RestResult(1L,resource);
	}
	
	/**
	 * 根据ID删除数据
	 * @param feedbackID 意见反馈的ID
	 */
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@RequestMapping(value = "/{feedbackID:\\d+}", method = RequestMethod.DELETE)
	public void deleteFeedbackByID(@PathVariable("feedbackID") long feedbackID){
		feedbackDao.deleteByID(feedbackID);
	}
	
	/**
	 * 根据ID批量删除意见反馈，多个ID之间用逗号分隔
	 * @param feedbackIDs ID
	 */
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	public void deleteFeedbackByIDs(@RequestParam String feedbackIDs){
		if(feedbackIDs==null || feedbackIDs.trim().length()==0){
			throw new CoreException(ErrorCode.PARAMETER_ERROR,"feedbackIDs参数不能为null");
		}
		List<Long> IDs = new LinkedList<>();
		String[] strs = feedbackIDs.split(",");
		for(String str:strs){
			if(str.matches("\\d+")){
				IDs.add(Long.valueOf(str));
			}
		}
		if(!IDs.isEmpty()){
			feedbackDao.deleteByID(IDs);
		}
	}
	
	/**
	 * 根据ID获得意见反馈
	 * @param request HTTP请求
	 * @param feedbackID ID
	 * @return 意见反馈
	 */
	@RequestMapping(value = "/{ID:\\d+}", method = RequestMethod.GET)
	public RestResult getFeedbackByID(HttpServletRequest request,@PathVariable("ID") long feedbackID){
		FeedbackParameter param = ParameterUtil.getParameter(request, FeedbackParameter.class);
		Feedback feedback = feedbackDao.getByID( feedbackID);
		FeedbackResource resource = null;
		long total = feedback==null?0:1;
		if(feedback != null){
			resource = resourceUtil.toFeedbackResource(feedback,param);
		}
		return new RestResult(total,resource);
	}
	
	/**
	 * 根据参数获得意见反馈的列表
	 * @param request http请求
	 * @return 意见反馈的列表
	 * @see studio.xiaoyun.core.parameter.FeedbackParameter 意见反馈的参数类
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public RestResult getFeedbackByParameter(HttpServletRequest request){
		FeedbackParameter param = ParameterUtil.getParameter(request, FeedbackParameter.class);
		long count = feedbackDao.getFeedbackCountByParameter(param);
		List<Feedback> feedbackList = feedbackDao.getFeedbacksByParameter(param);
		List<FeedbackResource> resourceList = resourceUtil.toFeedbackResource(feedbackList,param);
		return new RestResult(count,resourceList);
	}

}
